﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.Query;
using NBusiness.Data.Providers.SqlServer.Query;

namespace NBusiness.Data.Testing.Mocks
{
    class MockDatabaseProvider : IDatabaseProvider
    {
        #region IDatabaseProvider Members

        public IQuery Query()
        {
            return new SqlQuery();
        }

        public ITable Table(string name)
        {
            return new SqlTable(name);
        }

        public void Execute(IQuery query)
        {
            MethodCall.Calls.Add(new MethodCall(
                   "Execute",
                   new { Name = "query", Value = query }));

            SqlQuery sqlQuery = query as SqlQuery;
            
            // Simulate setting the identity parameter after an insert
            IParameterValue id = (sqlQuery.Statements[0] as IInsert).Parameters.FirstOrDefault(v => v.Name == "@Id");
            if (id!=null && id.Direction == ParameterValueDirection.Output)
            {
                id.Value = 100;
            }

            sqlQuery.OnLoad(new MockLoader());
        }

        #endregion
    }
}
